;(function() {
	
	// Constants for the two available search types
	
	Nokia.SEARCH_TYPE_GPS = 1;
	Nokia.SEARCH_TYPE_GEOCODE = 2;
	
	// All Views must extend Nokia.View
	
	Nokia.HomeView = Nokia.View.extend({
		
		renderUI: function() {
			var instance = this;
			
			// Build the introduction TextLabel
			
			var introLabel = new Nokia.TextLabel({
				element: '#intro-label',
				content: '#intro-label-content'
			});
			
			// Build the search Button

			this.buttonQuery = new Nokia.Button({
				element: '#home-query-button',
				label: 'Locate',
				width: '50%',
				click: function() {
					instance.doSearch();
				}
			});
			
			// Build the two Tooltip's used for warning messages
			
			this.locationTooltip = new Nokia.Tooltip({
				element: '#tooltip-location',
				description: 'Please specify your location'
			});
			this.searchTypeTooltip = new Nokia.Tooltip({
				element: '#tooltip-search-type',
				description: 'Please select a search type'
			});
			
			// Build the RadioBoxes and OptionGroup for the search type selection
			
			var radio1 = new Nokia.RadioBox({
				element: '#search-type1',
				label: 'Detect via GPS',
				check: function(event) {
					instance.setSearchType(Nokia.SEARCH_TYPE_GPS);
				}
			});
			var radio2 = new Nokia.RadioBox({
				element: '#search-type2',
				label: 'Select manually',
				check: function(event) {
					instance.setSearchType(Nokia.SEARCH_TYPE_GEOCODE);
				}
			});
	
			var optionGroup = new Nokia.OptionGroup({
				element: '#search-type-optiongroup',
				title: 'Where are you?',
				check: function(event) {

				}
			});
	
			optionGroup.addItem(radio1);
			optionGroup.addItem(radio2);


		},
		
		/*
		 * Method called when a RadioBox is clicked
		 */
		setSearchType: function(value)
		{
			this.searchType = value;
			
			// If the manual selection is selected, show the INPUT text-field
			
			if(this.searchType == Nokia.SEARCH_TYPE_GPS)
				$('#location-box').hide();
			else
				$('#location-box').show();
		},
		
		/*
		 * Method called when the search submit button is clicked
		 */
		doSearch: function()
		{
			if(!this.searchType)
			{
				// To programmatically show a Tooltip, call the click() method of its bound DOM node
				
				$('#tooltip-search-type').click();
				
				return;
			}
			else if(this.searchType == Nokia.SEARCH_TYPE_GPS)
			{
				this.locateMe();
			}
			else
			{
				this.searchLocation();
			}
		},
		
		/*
		 * Method called when a manual location search is submitted
		 */
		searchLocation: function()
		{
			
			var locationQuery = $('#location_field').val();
			
			if(locationQuery.length == 0)
			{
				$('#tooltip-location').click();
			}
			else
			{
				GBC.showLoading();
				
				var instance = this;
				
				Nokia.GBCLocation.geocodeLocation(locationQuery, function(errorCode, errorMessage, location) {
					instance.searchLocationHandler(errorCode, errorMessage, location);
				});
			}
		},
		
		/*
		 * Callback for the manual selection search
		 */
		searchLocationHandler: function(errorCode, errorMessage, location)
		{
			if(errorCode == 0)
			{
				this.startServicesSearch(location);
			}
			else
			{
				GBC.hideLoading();
				
				alert(errorMessage);
			}
		},
		
		/*
		 * Method called when GPS location search is submitted
		 */
		locateMe: function()
		{
			GBC.showLoading();
			
			var instance = this;
			
			Nokia.GBCLocation.locateDeviceLocation(
				function(errorCode, errorMessage, location)
				{
					instance.locateMeHandler(errorCode, errorMessage, location);
				}
			)
		},
		
		/*
		 * Callback for the GPS location search
		 */
		locateMeHandler: function(errorCode, errorMessage, location)
		{
			if(errorCode == 0)
			{
				this.startServicesSearch(location);
			}
			else
			{
				GBC.hideLoading();
				
				alert(errorCode + ", " + errorMessage);
			}
		},
		
		/*
		 * Starts the Web services query operation
		 */
		startServicesSearch: function(location)
		{
			var instance = this;

			Nokia.GBCServices.retrieveItems(
				location, 
				function(items){
					instance.resultsSearchSuccess(items)
				}, 
				function(){
					instance.resultsSearchError();
				}
			);
		},
		
		/*
		 * Error callback for the Web services query operation
		 */
		resultsSearchError: function()
		{
			GBC.hideLoading();
			
			alert("There was an error during search");
		},
		
		/*
		 * Success callback for the Web services query operation
		 */	
		resultsSearchSuccess: function(items)
		{
			GBC.hideLoading();
			
			this.getTemplate().getViewManager().getViews()[Nokia.VIEW_RESULTS].showResults(items);
			
			this.getTemplate().getViewManager().show(Nokia.VIEW_RESULTS);
		},
		
		/*
		 * Method called when this view is requested to be shown
		 */
		show: function()
		{
			this.getTemplate().getHeaderTopBar().setItems(GBC.getDefaultTopItems());
			
			this.getContainer().show();
		}
	});
})();